package com.lkc.controller;

import com.lkc.bean.Orders;
import com.lkc.service.OrdersService;
import com.lkc.util.Page;
import com.lkc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping
    public Result add(@RequestBody Orders orders) {
        Orders obj = ordersService.add(orders);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody Orders orders) {
        Orders obj = ordersService.edit(orders);
        return Result.success(obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer orderId) {
        int obj = ordersService.del(orderId);
        return Result.success(obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer orderId) {
        Orders obj = ordersService.one(orderId);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Orders orders, Page page) {
        ordersService.page(page, orders);
        return Result.success(page);
    }

    @GetMapping("all")
    public Result all(Orders orders) {
        List<Orders> list = ordersService.all(orders);
        return Result.success(list);
    }

    @GetMapping("buyer/{buyerId}")
    public Result findByBuyerId(@PathVariable("buyerId") Integer buyerId) {
        List<Orders> list = ordersService.findByBuyerId(buyerId);
        return Result.success(list);
    }
}
